package com.sjc.algorithm.queue;

/**
 * 循环队列
 * 使用数组实现
 * @author by SangJiacun
 * @Date 2020/5/11 11:57
 */
public class MyQueue {
    // 队列
    private int[] array;
    // 队头
    private int front;
    // 队尾
    private int rear;

    public MyQueue(int size){
        this.array = new int[size];
    }

    public static void main(String[] args) throws Exception {
        MyQueue myQueue = new MyQueue(5);

        // 入队
        myQueue.inQueue(1);
        myQueue.printQueue();
        myQueue.inQueue(2);
        myQueue.printQueue();
        myQueue.inQueue(3);
        myQueue.printQueue();
        myQueue.inQueue(4);
        myQueue.printQueue();
        myQueue.inQueue(5);
        myQueue.printQueue();
        myQueue.inQueue(6);

        //出队
        myQueue.outQueue();
        myQueue.printQueue();
        myQueue.outQueue();
        myQueue.printQueue();
        myQueue.outQueue();
        myQueue.printQueue();

        myQueue.inQueue(6);
        myQueue.printQueue();
        myQueue.inQueue(7);
        myQueue.printQueue();
        myQueue.inQueue(8);
        myQueue.printQueue();
        myQueue.inQueue(9);
        myQueue.printQueue();
    }

    /**
     * 入队
     *   分2种情况：
     *     1. front < rear: 对头在队尾前面，还没有循环
     *     2. front > rear: 对头在队尾后面，已经循环了
     * @param element
     */
    public void inQueue(int element) throws Exception {
        if(front == rear ){
            if(array[front] == 0){
                array[rear] = element;
                rear=1;
            }else{
                throw new Exception("对头=0，队尾="+rear+" , 队列已经满了!");
            }
        }else if(front < rear){
            array[rear] = element;
            rear = rear == (array.length-1)?0:rear++;
        }else{
            array[rear] = element;
            rear++;
        }
    }

    /**
     * 书中的代码
     * @param element
     * @throws Exception
     */
    public void enQueue(int element) throws Exception {
        if((rear+1)%array.length == front){
            throw new Exception(" 队列已满！");
        }
        array[rear] = element;
        rear =(rear+1)%array.length;
    }

    /**
     * 出队
     * @return
     * @throws Exception
     */
    public int outQueue() throws Exception {
        if(front == rear && array[front]==0){
            throw new Exception("队列为空!!!!");
        }
        int element = array[front];
        front = front<(array.length-1)?front+1:0;
        return element;
    }

    /**
     * 书中的代码
     * @return
     * @throws Exception
     */
    public int deQueue() throws Exception {
        if(rear == front){
            throw new Exception(" 队列已空！");
        }
        int deQueueElement = array[front];
        front =(front+1)%array.length;
        return deQueueElement;
    }

    /**
     * 输出
     */
    public void printQueue(){
        StringBuilder sb = new StringBuilder();
        sb.append(" 对头 --> "+front);
        sb.append(" 队尾 --> "+rear);
        sb.append(" [ ");
        if(front < rear){
            for(int i=front;i<rear;i++){
                sb.append(array[i]+" ");
            }
        }else{
            for(int i=front;i<array.length;i++){
                sb.append(array[i]+" ");
            }
            for(int i =0;i<rear;i++){
                sb.append(array[i]+" ");
            }
        }
        sb.append(" ]");
        System.out.println(sb.toString());
    }

    /**
     * 书中的代码
     */
    public void output(){
        for(int i=front; i!=rear; i=(i+1)%array.length){
            System.out.println(array[9]);
        }
    }
}
